﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMining.Bean;
using System.Data.SqlClient;
using DataMining.DataAccess.HospitalDataSetTableAdapters;
using System.Data;
using System.Windows;

namespace DataMining.DataAccess
{
    class CIS_Operation_DetailAccess
    {

//GetTable和InsertList成对出现，保障从数据源传出的create_time和传入目标数据库的create_time一致
        private DateTime create_time;
        private DateTime end_time;

        /// <summary>
        /// 统一取时间步进
        /// </summary>
        /// <param name="crtime"></param>
        /// <returns></returns>
        public DateTime Add_Create_Time(DateTime create_time)
        {
            DateTime end_time;
            end_time = create_time.AddHours(6);
            return end_time;
        }

        /// <summary>
        /// 从数据源数据库获得数据
        /// </summary>
        /// <param name="create_time"></param>
        /// <returns></returns>
        public DataTable GetTable(DateTime cre_time)//,String atrribute
        {
            create_time = cre_time;
            this.end_time = this.Add_Create_Time(create_time);

            //通过单例模式获取系统唯一的connection
            DBConnection dbconn = new DBConnection();
            SqlConnection connection1 = dbconn.getSqlConnection1();
            connection1.Open();

            //设置command
            SqlCommand command = connection1.CreateCommand();
            DateTime end_time = this.Add_Create_Time(create_time);
            command.CommandText = "select * from inpatient_medical_record_operation_item_MDT where (create_time >" + "'" + create_time + "'" + " and create_time<=" + "'" + end_time.ToString() + "')"+ "order by create_time";
          
            //定义Adapter
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.SelectCommand = command;

            //定义数据Table
            DataTable dbTable = new DataTable();
            adapter.Fill(dbTable);//装载数据表
            connection1.Close();
            return dbTable;
        }
           
           
        public void InsertList(List<CIS_Operation_Detail> detaillist)
            //通过单例模式获取系统唯一的connection
        {
            MyConnection myconn = new MyConnection();
            SqlConnection connection = myconn.getSqlConnection2();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select * from CIS_Operation_Detail where CJSJ >" + "'" + create_time + "'" + " and CJSJ<=" + "'" + end_time.ToString() + "'";
         
            //定义Adapter
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
            adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
            //adapter.InsertCommand = commandBuilder.GetInsertCommand();
            //adapter.DeleteCommand = commandBuilder.GetDeleteCommand();

            //定义数据Table
            DataTable detailTable = new DataTable("CIS_Operation_Detail");
            adapter.Fill(detailTable);
            int count = 0;
            //设置TDS的数据库每次提交的数量
            int batchsiza = 1000;
            adapter.UpdateBatchSize = batchsiza;
            foreach (CIS_Operation_Detail detail in detaillist)
            {
                count++;
                DataRow row = detailTable.NewRow();
                //通过反射机制填入row中
                RowValueFromDataTable.GetValueFromTable(detail, row);
                detailTable.Rows.Add(row);


                //当count达到batchsiza的整数倍，或者达到最后一批存入要求
                if (count % batchsiza == 0 || count == detaillist.Count)
                {
                    try 
                    {
                        adapter.Update(detailTable);
                    }
                    catch (SqlException ex) 
                    {
                        if (ex.Number != 2627)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    finally 
                    {
                        detailTable.Clear();
                    }
                    
                    
                }
            }
            adapter.Update(detailTable);
            detailTable.Clear();
            connection.Close();
        }
    }
}
